﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using VietNamKey.Properties;

namespace VietNamKey
{
    public partial class frmMain : Form
    {
        private const string LastSettingPath = "LastSetting.xml";
        public frmMain()
        {
        	CheckForIllegalCrossThreadCalls = false;
            if (!File.Exists(LastSettingPath))
            {
                Global.LoadDefaultSetting();
            }
            else
            {
                try
                {
                    Global.KeySettings = ObjectXMLSerializer<KeySettings>.Load(LastSettingPath);
                }
                catch { Global.LoadDefaultSetting(); }
            }
            //=======================================================================//
            InitializeComponent();
            this.Icon = Resources.main;
            trayIcon.Icon = Resources.V;
            var hook = new UserActivityHook(true, true);
            hook.OnMouseActivity += MyMouseActivity;
            hook.KeyDown += MyKeyDown;
            hook.KeyPress += MyKeyPress;
            hook.KeyUp += MyKeyUp;
            CheckForIllegalCrossThreadCalls = false;
            //======================================================================//
            tslTypingMethod.Items.AddRange(TypingMethods.List);
            tslEncoding.Items.AddRange(Encodings.List);
            UpdateFromSetting();
        }
        protected override void SetVisibleCore(bool value)
        {
            var tmp = WindowState;
            if (value) WindowState = FormWindowState.Normal;
            if (!Global.KeySettings.OpenDialogWhenStart && tmp != FormWindowState.Normal)
            {
                value = false;
                if (!this.IsHandleCreated) CreateHandle();
            }
            base.SetVisibleCore(value);
        }
        private void UpdateFromSetting()
        {
            tslTypingMethod.SelectedIndex = Global.KeySettings.TypingMethod;
            tslEncoding.SelectedIndex = Global.KeySettings.Encoding;
            tscUseShorthand.Checked = Global.KeySettings.UseShorthand;
            tscShorthandInEN.Checked = Global.KeySettings.UseShorthandInEn;
            tscUseNewStyle.Checked = Global.KeySettings.UseNewStyle;
            tscUseSendKey.Checked = Global.KeySettings.UseSendKey;
            tscUseVNInAutocomplete.Checked = Global.KeySettings.UseVnInAutoComplete;
            tslCheckSpelling.Checked = Global.KeySettings.UseCheckSpelling;
            tscRunAtWindowsStart.Checked = Global.KeySettings.RunAtWindowsStart;
        }

        private void trayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                SwitchMode();
            }
        }

        private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show(); this.Focus();
        }

        private void tsmExit_Click(object sender, EventArgs e)
        {
            trayIcon.Visible = false;
            try{
            ObjectXMLSerializer<KeySettings>.Save(Global.KeySettings, LastSettingPath);
            } catch{}
            Environment.Exit(0);
        }

        private void tsmOptions_Click(object sender, EventArgs e)
        {
            ucGeneral1.Reload();
            ucAdvanced1.Reload();
            ucShorthand1.Reload();
            this.Show(); this.Focus();
            btnGeneral.PerformClick();
        }

        private void tsmRunAtWindowsStart_CheckedChanged(object sender, EventArgs e)
        {
            RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            if (tscRunAtWindowsStart.Checked)
            {
                rkApp.SetValue("VietNamKey", Application.ExecutablePath);
            }
            else
            {
                rkApp.DeleteValue("VietNamKey", false);
            }
        }

        private void tscUseVNInAutocomplete_CheckedChanged(object sender, EventArgs e)
        {
            Global.KeySettings.UseVnInAutoComplete = tscUseVNInAutocomplete.Checked;
        }

        private void tscUseNewStyle_CheckedChanged(object sender, EventArgs e)
        {
            Global.KeySettings.UseNewStyle = tscUseNewStyle.Checked;
        }
        
        private void tslCheckSpelling_CheckedChanged(object sender, EventArgs e)
        {
        	Global.KeySettings.UseCheckSpelling = tslCheckSpelling.Checked;
        }

        private void tscUseSendKey_CheckedChanged(object sender, EventArgs e)
        {
            Global.KeySettings.UseSendKey = tscUseSendKey.Checked;
        }

        private void tslTypingMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tslTypingMethod.SelectedItem != null)
            {
                Global.KeySettings.TypingMethod = tslTypingMethod.SelectedIndex;
            }
        }

        private void tslEncoding_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tslEncoding.SelectedItem != null)
                Global.KeySettings.Encoding = tslEncoding.SelectedIndex;
        }

        private void tmsConvert_Click(object sender, EventArgs e)
        {
            this.Show(); this.Focus();
            btnConvert.PerformClick();
        }

        private void tsmHelp_Click(object sender, EventArgs e)
        {
            this.Show();this.Focus();
            btnAbout.PerformClick();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void tscUseShorthand_Click(object sender, EventArgs e)
        {
            Global.KeySettings.UseShorthand = tscUseShorthand.Checked;
        }

        private void tscShorthandInEN_Click(object sender, EventArgs e)
        {
            Global.KeySettings.UseShorthandInEn = tscShorthandInEN.Checked;
        }

        private void tsmShorthandOption_Click(object sender, EventArgs e)
        {
            this.Show();this.Focus();
            btnShorthand.PerformClick();
        }

        private void btnTab_Click(object sender, EventArgs e)
        {
            tbcMain.SelectedIndex = tabStrip.Items.IndexOf(sender as ToolStripItem);
        }

        private void ucGeneral1_SettingChanged(object sender, EventArgs e)
        {
            UpdateFromSetting();
        }

        private void ucAdvanced1_SettingChanged(object sender, EventArgs e)
        {
            UpdateFromSetting();
        }
    }
}